<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
	$return['pluginset']['hdps']=$return['pluginset']['canghdps'];
	$return['pluginset']['bankuai']=$return['pluginset']['cangbankuai'];
	$return['sale_red']=$this->pdo_getall('feelog',array('type'=>2,'status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	foreach($return['sale_red'] as &$sv){
		$sv['member']=cfc('core')->member_show($sv['mid']);
		$sv['createtime']=date('Y-m-d H:i',$sv['createtime']);
	}
	$return['red']=$_SESSION['member'];
	$return['red']['allfee']=$this->pdo_getsum('feelog',array('type'=>2,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$return['red']['isfee']=$this->pdo_getsum('feelog',array('type'=>2,'status'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$return['red']['lastfee']=$this->pdo_getsum('feelog',array('type'=>2,'status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$return['red']['red_mp3']=$return['pluginset']['red_mp3'];
	$return['groups']=pdo_getall('jy_robshop_goods_groups',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'));
	$return['params']['groupid']=0;
	$sharelink='jy_robshop/cang//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$this->setting['share_title']?:'抢购商城-在线抢购',
			'desc'=>$this->setting['share_desc']?:'抢购商城，参与抢购！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>ATTACHMENT_URL.$return['pluginset']['logo']
	);
	set_html('title','抢购商城');
	exi($return);
}
if($_RQ['op']=='getlist'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['keyword']){
		$params['title like']='%;'.$_RQ['keyword'].';%';
	}
	if($_RQ['groupid']){
		$params['groupid']=$_RQ['groupid'];
	}
    $orderby='createtime desc';
	$total='';
	$list=pdo_getslice('jy_robshop_goods',$params,array($pindex,$psize),$total,'','','id desc');
	if($list){
		$day=date('Y/m/d');
		$hourkey=$this->model('model')->gethourkey();
		foreach($list as &$item){
			$myhourkey=$hourkey;
			if(!$item['goodstype']){
				$item['goods']=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),array('title','price','image'));
				$item['image']=$item['goods']['image'];
			}
			if($item['is_vipbuy'] && $item['vipprice']>0){
				$item['isvip']=$this->model('team')->check_vipbuy(0,$item);
			}
			$item['salenum']=pdo_count('jy_robshop_canlog',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$item['id']),'id');
			$item['statustitle']=$this->goods_status[$item['status']];
			if($item['status']==1){
				//全天
				if($item['jian_openday']){
					$rest_time=$item['createtime']+$item['jian_days']*24*3600;
					if($rest_time<time()){
						$rest_time=strtotime($day.' 24:00:00');
					}
					$item['hourtype']=1;
					$this->model('model')->up_yue2canlog($item);
				}else{
					$item['hours']=json_decode($item['jian_hour'.$myhourkey],true);
					$start_time=strtotime($day.' '.$item['hours']['start']);
					$end_time=strtotime($day.' '.$item['hours']['end']);
					if(TIMESTAMP>$end_time){
						if($myhourkey!=2){
							$myhourkey ++;
							$item['nexthours']=json_decode($item['jian_hour'.$myhourkey],true);
							$start_time=strtotime($day.' '.$item['nexthours']['start']);
							$item['hourtype']=0;
							$rest_time=$start_time;
						}else{
							$item['hourtype']=2;
							$rest_time='';
						}
					}elseif(TIMESTAMP>$start_time){
						$item['hourtype']=1;
						$rest_time=$end_time;
						$this->model('model')->up_yue2canlog($item);
					}else{
						$item['hourtype']=0;
						$rest_time=$start_time;
					}
				}
				if($item['hourtype']!=1){
					$this->model('model')->up_can2success($item);
				}
				$item['hourkey']=$this->hourkey[$myhourkey];
				if($rest_time){
					$item['rest_time']=model('jy_weishop/model')->date($rest_time-TIMESTAMP);
				}
			}
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_robshop_canlog',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$item['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$item['members']=$members;
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='getred'){
	$post=get_postdata();	
	$id=intval($post['id']);
	$sale_reds=$this->pdo_getall('feelog',array('type'=>2,'status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if(empty($sale_reds)){
		exi('没有红包可领取！','error');
	}
	foreach($sale_reds as $sv){
		if($sv['fee']>0){
			$red=array(
				'mid'=>$_SESSION['uid'],
				'money'=>$sv['fee'],
				'plugin'=>'jy_robshop',
				'remark'=>'参与抢购红包提现',
			);
			model('core/red_pack')->get_money($red);
		}
	}
	$this->pdo_update('feelog',array('status'=>1),array('type'=>2,'status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('领取成功！');
}
?>